package com.sjy.shopmall.controller;

import com.sjy.shopmall.service.CartService;
import com.sjy.shopmall.utils.SysUtils;
import com.sjy.shopmall.vo.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("cart")
public class CartController {
    @Resource
    CartService cartService;


    @Resource
    SysUtils sysUtils;
    @GetMapping("cartList")
    ResultVO info() {
        Long userid=sysUtils.getLoginedUserID();
        if(userid==null)
        {
            throw new RuntimeException("请登陆后重试");
        }
        return new ResultVO(ResultVO.success, null, cartService.findUserCartsDetails(userid));
    }


    @GetMapping("updateCartCount")
    ResultVO update(Long id, BigDecimal count) {
        Long userid=sysUtils.getLoginedUserID();
        if(userid==null)
        {
            throw new RuntimeException("请登陆后重试");
        }
        cartService.updateCount(id,count);
        return new ResultVO(ResultVO.success, null, cartService.findUserCartsDetails(userid));
    }
    @GetMapping("addCart")
    ResultVO add(Long good) {
        Long userid=sysUtils.getLoginedUserID();
        if(userid==null)
        {
            throw new RuntimeException("请登陆后重试");
        }
        cartService.addCart(userid, good);//实际参数 根据形参来给=》形参也必须跟实参类型一样
        return new ResultVO(ResultVO.success, null, null);
    }
}
